package com.meihua.shellapp.activity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dou361.ijkplayer.listener.OnShowThumbnailListener;
import com.dou361.ijkplayer.widget.PlayStateParams;
import com.dou361.ijkplayer.widget.PlayerView;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseWebViewActivity;
import com.meihua.shellapp.utils.URL;
import com.orhanobut.logger.Logger;
import com.squareup.picasso.Picasso;

/**
 * 创客中心 微课堂 产品知识界面
 */
public class ClassRoomDetailActivity extends BaseWebViewActivity {
    TextView tvTitle;
    WebView webView;
    ImageView mIVTopLeft;

    private String id;//微课堂 分类 id
    private String study_url;//微课堂 分类 id
    private String attachment_type;//微课堂 分类 id
    private PlayerView player;
    private View topView;
    private View videoView;
    private ImageView iv_bgbg;
    private ImageView video_fullscreen;

    @Override
    public int getLayoutId() {
        return R.layout.activity_class_room_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        /*设置布局 并初始化*/
        tvTitle = (TextView) findViewById(R.id.tv_title);
        webView = (WebView) findViewById(R.id.common_problem_webview);
        topView = findViewById(R.id.layouttop);
        videoView = findViewById(R.id.app_video_box);
        mIVTopLeft = (ImageView) findViewById(R.id.iv_top_left);
        iv_bgbg = (ImageView) findViewById(R.id.iv_bgbg);
        video_fullscreen = (ImageView) findViewById(R.id.app_video_fullscreen);
        ImageView iv_trumb = (ImageView) findViewById(R.id.iv_trumb);
        findViewById(R.id.app_video_stream).setVisibility(View.GONE);
        findViewById(R.id.simple_player_volume_controller_container).setVisibility(View.GONE);
        findViewById(R.id.simple_player_brightness_controller_container).setVisibility(View.GONE);
        findViewById(R.id.ijk_iv_rotation).setVisibility(View.GONE);
        ImageView videofinish = (ImageView) findViewById(R.id.app_video_finish);
        videofinish.setVisibility(View.GONE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        mIVTopLeft.setOnClickListener(this);
        id = getIntent().getStringExtra("id");//id
        tvTitle.setText(getIntent().getStringExtra("title"));
        tvTitle.setVisibility(View.VISIBLE);
        study_url = getIntent().getStringExtra("study_url");//视频URL
        attachment_type = getIntent().getStringExtra("attachment_type");//发布类型：1=>视频 2=>文档，3=>音频
        /*初始化webview*/
        initWebView(webView, null, URL.getUrl().CLASSDETAILS + id,tvTitle);

        if (attachment_type.equals("1") || attachment_type.equals("3")) {
            videoView.setVisibility(View.VISIBLE);
        } else {
            videoView.setVisibility(View.GONE);
        }
        if (!attachment_type.equals("2") && study_url != null) {
            if (attachment_type.equals("3")) {
//                iv_bgbg.setVisibility(View.VISIBLE);
                video_fullscreen.setVisibility(View.INVISIBLE);
                video_fullscreen.setClickable(false);
            } else {
                iv_bgbg.setVisibility(View.GONE);
                video_fullscreen.setClickable(true);
                video_fullscreen.setVisibility(View.GONE);
            }
            //视频
            playerVideo(attachment_type);
        }
    }

    /*播放视频*/
    private void playerVideo(String attachment_type) {
        player = new PlayerView(this, null)
                .setScaleType(PlayStateParams.fitparent)
                .hideMenu(true)
                .forbidTouch(false)
                .setForbidDoulbeUp(true)
                .hideFullscreen(attachment_type.equals("3"))
                .showThumbnail(new OnShowThumbnailListener() {
                    @Override
                    public void onShowThumbnail(ImageView ivThumbnail) {
                        Picasso.with(ClassRoomDetailActivity.this)
                                .load(android.R.color.black)
                                .error(android.R.color.black)
                                .into(ivThumbnail);
                    }
                })
                .setPlaySource(URL.getUrl().HOST + study_url)
                .startPlay();
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (player != null) {
            player.onResume();
        }
    }

    /**
     * 销毁
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.onDestroy();
        }
    }

    /**
     * 当屏幕配置变化时
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Logger.i("info", "横屏"); // 横屏
            if (topView != null) topView.setVisibility(View.GONE);
        } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            Logger.i("info", "portrait"); // 竖屏
            if (topView != null) topView.setVisibility(View.VISIBLE);
        }
        if (player != null) {
            player.onConfigurationChanged(newConfig);
        }
    }

    /**
     * 返回键按下的操作
     */
    @Override
    public void onBackPressed() {
        if (player != null && player.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
